window._ME={

    _INDEX:0,

    _NAME:null,

    _SEARCHNAME:null,

    _EXPLAINCONFIG:{},

    _ARMYMAP:null,

    _SKILLMAP:null,

    _NPCPLACE:null,

    _HEADPAGE:'名词解释',

    getNpcPlaceMap:function(){

        if(!_ME._NPCPLACE){

            _ME._NPCPLACE={};

            for(var shop in _SHOP){

                if(_SHOP[shop].type=="tzs"){

                    var shopsell = _SHOP[shop].sell;

                    for(var i=0;i<shopsell.length;i++){

                        for(var j=0;j<shopsell[i].length;j++){

                            if(shopsell[i][j]){

                                var npcname = shopsell[i][j].replace("挑战书","");

                                _ME._NPCPLACE[npcname]=[shop+(i+1)];
                            }else{
                                //console.log(shop,i,j,shopsell[i][j]);
                            }
                        }

                    }

                }

            }

            for(var map in _CMAP){

                for(var i=0;i<_CMAP[map].boss.length;i++){
                    var bossname = _CMAP[map].boss[i];
                    if(!_ME._NPCPLACE[bossname]){
                        _ME._NPCPLACE[bossname] = [map];
                    }else{
                        _ME._NPCPLACE[bossname].push(map);
                    }

                }

            }

        }

    },

    getNpcLiupai:function(npc){

        _ME.getNpcPlaceMap();

        if(_ME._NPCPLACE[npc]&&_ME._NPCPLACE[npc].length>=1){
            var liupai = _ME._NPCPLACE[npc][0];
            var liupaiarr = liupai.split("挑战");
            if(liupaiarr.length==2){
                return liupaiarr[0];
            }
        }

        return "未知";

    },

    getNpcPlace:function(npc){

        _ME.getNpcPlaceMap();

        if(_ME._NPCPLACE[npc]){
            return _ME._NPCPLACE[npc].join(',');
        }
        return "未知";
    },

    initMap:function(){
        if(!_ME._ARMYMAP){
            _ME._ARMYMAP={};
            _ME._SKILLMAP={};

            for(var key in _NPC){

                var npclog = _GAMEDATAHELPER.getNpcLog(key);

                if(npclog&&npclog.city){
                    _ME._ARMYMAP[key]=[npclog.city];

                    if(_NPC[key].skill&&_NPC[key].skill.length>0){
                        for(var i=0;i<_NPC[key].skill.length;i++){
                            var tmpskill = _NPC[key].skill[i];
                            if(!_ME._SKILLMAP[tmpskill]){
                                _ME._SKILLMAP[tmpskill]=[];
                            }
                            _ME._SKILLMAP[tmpskill].push(key+"("+npclog.city+")");
                        }
                    }

                }

            }

            for(var key in _AREAARMY){

                for(var i=0;i<_AREAARMY[key].animals.length;i++){

                    var animalsname = _AREAARMY[key].animals[i];

                    if(!_ME._ARMYMAP[animalsname]){
                        _ME._ARMYMAP[animalsname]=[];
                    }

                    _ME._ARMYMAP[animalsname].push(key);

                }

                for(var i=0;i<_AREAARMY[key].boss.length;i++){

                    var animalsname = _AREAARMY[key].boss[i];

                    if(!_ME._ARMYMAP[animalsname]){
                        _ME._ARMYMAP[animalsname]=[];
                        _ME._ARMYMAP[animalsname].push(key);
                    }

                }

            }
        }

    },

    showMe:function(){

        var uiObj = _GRM.getNodeObj('explainLabel');
        var uiNode = uiObj.node;

        if(uiObj.isnew){
            _ME.initButton(uiNode);
            _ME.refreshJobDetail(uiNode);
        }

        _ME.initMap();

        _ME._EXPLAINCONFIG[_ME._HEADPAGE]=null;

        _ME.showExplainDetailByConfig(_ME._HEADPAGE,uiNode);

        _MAP.node.addChild(uiNode);
    },

    refreshJobDetail:function(uiNode){

        var objArr = uiNode.getChildByName("explain").getChildByName("scrollview").getChildByName("view")
              .getChildByName("content").getChildByName("职业一览").getChildByName("职业").getChildren();
        for(var i=0;i<objArr.length;i++){

            var tobjArr = objArr[i].getChildren();

            for(var j=0;j<tobjArr.length;j++){

                var tobj = tobjArr[j];
                var tobjname = tobj.getName();
                var skillbdObj = tobj.getChildren()[1];
                if(_JOB[tobjname]){

                    

                    var skillbdArr = _CROF.getSkillOfJob(tobjname);

                    if(skillbdArr&&skillbdArr.length>0){
                        var attr = [];
                        for(var k=0;k<skillbdArr.length;k++){

                            if(_SKILLBD[skillbdArr[k]]){

                                for(var key in _SKILLBD[skillbdArr[k]].attr){

                                    attr.push(_DICT[key]);

                                }

                            }

                        }
                        skillbdObj.getComponent(cc.Label).string = attr.join(",");
                    }
                    
                    
                }else{
                    //console.log(tobjname);
                }
            }

        }

    },

    initButton:function(uiNode){
        uiNode.getChildByName('mask').on(cc.Node.EventType.TOUCH_START, function(){
            _GRM.putNode(this);
        }, uiNode);

        var buttons = uiNode.getChildByName('tab').getChildByName('view').getChildByName('content').getChildren();

        var pageButtons = uiNode.getChildByName('explain').getChildByName('scrollview')
                            .getChildByName('view').getChildByName('content')
                            .getChildByName('currency').getChildByName('page');
        var pageUp = pageButtons.getChildByName('buttonUp');
        var pageDown = pageButtons.getChildByName('buttonDown');
        var searchBtn = uiNode.getChildByName('explain').getChildByName('scrollview')
                        .getChildByName('view').getChildByName('content')
                        .getChildByName('currency').getChildByName('searchBtn');

        searchBtn.uinode = uiNode;
        searchBtn.on(cc.Node.EventType.TOUCH_START, function(){
                
            var searchtext = this.uinode.getChildByName('explain').getChildByName('scrollview')
                        .getChildByName('view').getChildByName('content')
                        .getChildByName('currency').getChildByName('searchInput').getComponent(cc.EditBox).string;

            //console.log('searchtext',searchtext);

            _ME.searchConfig(searchtext,this.uinode);

        }, searchBtn);

        pageUp.uinode = uiNode;
        pageUp.on(cc.Node.EventType.TOUCH_START, function(){
                
            //console.log('上一页');

            var config = _ME.getConfigByName(_ME._NAME);
            if(config[_ME._INDEX-1]){
                _ME._INDEX--;
                _ME.refreshDetailContent(config[_ME._INDEX],uiNode,config.length);
            }

        }, pageUp);

        pageDown.uinode = uiNode;
        pageDown.on(cc.Node.EventType.TOUCH_START, function(){
                
            //console.log('下一页');
            var config = _ME.getConfigByName(_ME._NAME);
            if(config[_ME._INDEX+1]){
                _ME._INDEX++;
                _ME.refreshDetailContent(config[_ME._INDEX],uiNode,config.length);
            }

        }, pageDown);

        for(var i=0;i<buttons.length;i++){

            buttons[i].uinode = uiNode;

            buttons[i].on(cc.Node.EventType.TOUCH_START, function(){
                
                if(!_ME.showExplainDetail(this.name,this.uinode)){
                    _ME.showExplainDetailByConfig(this.name,this.uinode);
                }

            }, buttons[i]);

        }
    },

    showExplainDetail:function(name,uiNode){

        var views = uiNode.getChildByName('explain').getChildByName('scrollview')
                    .getChildByName('view').getChildByName('content')
                    .getChildren();
        
        var flag = false;
        
        for(var i=0;i<views.length;i++){
            if(views[i].name==name){
                views[i].active = true;
                flag = true;
            }else{
                views[i].active = false;
            }
        }

        return flag;
    },

    getXWsearchKey:function(keyword){
        for(var key in _XW){
            if(_XW[key].label==keyword){
                return key;
            }
        }
        return null;
    },

    getFSXsearchKey:function(keyword){
        for(var key in _DICT){
            if(_DICT[key]==keyword){
                return key;
            }
        }
        return null;
    },

    getStatussearchKey:function(keyword){
        for(var key in _STATUSCONFCHESS){
            if(_STATUSCONFCHESS[key].name==keyword){
                return key;
            }
        }
        return null;
    },

    searchConfig:function(keyword,uiNode){

        var searchName = _ME._NAME;

        if(_ME._SEARCHNAME){
            searchName = _ME._SEARCHNAME;
            _ME._NAME = _ME._SEARCHNAME;
        }

        console.log('搜索项目:',searchName);

        var config = _ME.getConfigByName(searchName);

        var searchArr = [];

        //根据副属性搜索
        if(
            _ME._NAME=="被动技能"
            ||_ME._NAME=="装备大全"
            ||_ME._NAME=="职业大全"
        ){
            var xwkey = _ME.getFSXsearchKey(keyword);

            console.log('副属性搜索:',xwkey);

            if(xwkey){

                for(var i=0;i<config.length;i++){

                    var tempkey = config[i].name;

                    if(
                        _SKILLBD[tempkey]
                        &&_SKILLBD[tempkey].attr[xwkey]
                    ){
                        searchArr.push(config[i]);
                    }else if(
                        _EQUIP[tempkey]
                        &&_EQUIP[tempkey].attr[xwkey]
                    ){
                        searchArr.push(config[i]);
                    }else if(_JOB[tempkey]){
                        var jobskill = _CROF.getSkillOfJob(tempkey);
                        if(jobskill&&jobskill.length>0){
                            if(
                                _SKILLBD[jobskill[0]]
                                &&_SKILLBD[jobskill[0]].attr[xwkey]
                            ){
                                searchArr.push(config[i]);
                            }
                        }
                        
                    }
        
                }

            }
        }

        //根据修为搜索
        if(
            _ME._NAME=="被动技能"
            ||_ME._NAME=="主动技能"
            ||_ME._NAME=="装备大全"
            ||_ME._NAME=="职业大全"
        ){
            var xwkey = _ME.getXWsearchKey(keyword);

            console.log('修为搜索:',xwkey);

            if(xwkey){

                for(var i=0;i<config.length;i++){

                    var tempkey = config[i].name;

                    if(
                        _SKILLBD[tempkey]
                        &&_SKILLBD[tempkey].xw
                        &&_SKILLBD[tempkey].xw[xwkey]
                    ){
                        searchArr.push(config[i]);
                    }else if(
                        _EQUIP[tempkey]
                        &&_EQUIP[tempkey].xw
                        &&_EQUIP[tempkey].xw[xwkey]
                    ){
                        searchArr.push(config[i]);
                    }else if(
                        _JOB[tempkey]
                        &&_JOB[tempkey].xw
                        &&_JOB[tempkey].xw[xwkey]
                    ){
                        searchArr.push(config[i]);
                    }else if(
                        _SKILL[tempkey]
                        &&_SKILL[tempkey].xw
                        &&_SKILL[tempkey].xw[xwkey]
                    ){
                        searchArr.push(config[i]);
                    }
        
                }

            }
        }

        //根据状态搜索主动技能
        if(
            _ME._NAME=="主动技能"
        ){
            var xwkey = _ME.getStatussearchKey(keyword);

            console.log('状态搜索:',xwkey);

            if(xwkey){

                for(var i=0;i<config.length;i++){

                    var tempkey = config[i].name;

                    if(
                        _SKILL[tempkey]
                        &&(
                            _SKILL[tempkey].status==xwkey
                            ||_SKILL[tempkey].selfstatus==xwkey
                        )
                    ){
                        searchArr.push(config[i]);
                    }
        
                }

            }
        }



        for(var i=0;i<config.length;i++){

            if(_CU.like(keyword,config[i].name)){

                //_ME._INDEX = i;

                //_ME.refreshDetailContent(config[i],uiNode,config.length);

                //return;

                searchArr.push(config[i]);
            }

        }

        if(searchArr.length>0){
            _ME._NAME="关键字搜索";
            _ME._EXPLAINCONFIG['关键字搜索']=searchArr;
            _ME._INDEX = 0;
            _ME.refreshDetailContent(searchArr[0],uiNode,searchArr.length);
        }else{
            _TIPS.showMessage('查询不到匹配的结果',0);
        }
    },

    showExplainDetailByConfig:function(name,uiNode){

        var views = uiNode.getChildByName('explain').getChildByName('scrollview')
                    .getChildByName('view').getChildByName('content')
                    .getChildren();
        
        for(var i=0;i<views.length;i++){
            if(views[i].name=="currency"){
                views[i].active = true;
            }else{
                views[i].active = false;
            }
        }

        _ME._INDEX = 0;
        _ME._NAME = name;
        _ME._SEARCHNAME = name;
        var config = _ME.getConfigByName(name);
        if(config&&config.length>0)
        _ME.refreshDetailContent(config[0],uiNode,config.length);

    },

    refreshDetailContent:function(content,uiNode,total){

        var view = uiNode.getChildByName('explain').getChildByName('scrollview')
                    .getChildByName('view').getChildByName('content')
                    .getChildByName('currency');
        view.getChildByName('label').getComponent(cc.RichText).string = content.name+"\n"+content.content;
        if(total>1){
            view.getChildByName('page').active = true;
            view.getChildByName('page').getChildByName('pageLabel')
            .getComponent(cc.Label).string=(_ME._INDEX+1)+"/"+total;
        }else{
            view.getChildByName('page').active = false;
        }
    },

    getConfigByName:function(name){

        if(_ME._EXPLAINCONFIG[name]){
            return _ME._EXPLAINCONFIG[name];
        }else{

            _ME._EXPLAINCONFIG[name]=[];

            if(name=="游戏目标"){

                _ME._EXPLAINCONFIG[name] = _PLAYERSTATUS.getGameEndTypeStr();

            }else if(name=="按键说明"){
                _ME._EXPLAINCONFIG[name].push({name:'按键说明',content:_MK.showKeyInfo()});
            }else if(name=="名词解释"){

                for(var i=0;i<_HELPERDOC.length;i++){
                    _ME._EXPLAINCONFIG[name].push({name:_HELPERDOC[i][0],content:_HELPERDOC[i][1]})
                }

            }else if(name=="状态大全"){

                for(var key in _STATUSCONFCHESS){

                    var tempContent = _STATUSCONFCHESS[key].des;

                    for(var fkey in _FTYPESTATUSDEFEND){
                        if(_FTYPESTATUSDEFEND[fkey][key]){
                            tempContent+="\n"+fkey+"属性角色免疫此状态";
                        }
                    }

                    _ME._EXPLAINCONFIG[name].push({name:_STATUSCONFCHESS[key].name,content:tempContent})
                }

            }else if(name=="BOSS大全"){

                for(var key in _NPC){
                    _ME._EXPLAINCONFIG[name].push({name:key,content:_ME.getBossDesc(key)})
                }

            }else if(name=="被动技能"){

                var skillobj = null;

                for(var key in _SKILLBD){

                    skillobj=_IOF.createObjByName(key,null);
                    var baseInfo = _IOF.getGoodsDetailStr(skillobj);
                    /*
                    if(_SKILLBD[key].bossflag){
                        var npcname = _SKILLBD[key].bossflag.replace("专属","");
                        var npcplace = _ME.getNpcPlace(npcname);
                        if(_SKILLBD[key].bossflag.indexOf("专属")==-1){
                            baseInfo+="\n掉落:"+_SKILLBD[key].bossflag;
                        }else{
                            baseInfo+="\n"+_SKILLBD[key].bossflag+" 不可掉落";
                        }
                        if(npcplace){
                            baseInfo+="\n地点:"+npcplace;
                        }
                    }*/
                    _ME._EXPLAINCONFIG[name].push({name:key,content:baseInfo});

                }

            }else if(name=="主动技能"){

                var skillobj = null;

                for(var key in _SKILL){

                    skillobj=_IOF.createObjByName(key,null);
                    var baseInfo = _IOF.getGoodsDetailStr(skillobj);
                    /*
                    if(_SKILL[key].bossflag){
                        var npcname = _SKILL[key].bossflag.replace("专属","");
                        var npcplace = _ME.getNpcPlace(npcname);
                        if(_SKILL[key].bossflag.indexOf("专属")==-1){
                            baseInfo+="\n掉落:"+_SKILL[key].bossflag;
                        }else{
                            baseInfo+="\n"+_SKILL[key].bossflag+" 不可掉落";
                        }
                        if(npcplace){
                            baseInfo+="\n地点:"+npcplace;
                        }
                    }
                    */
                    _ME._EXPLAINCONFIG[name].push({name:key,content:baseInfo});

                }

            }else if(name=="职业大全"){

                for(var key in _JOB){
                    _ME._EXPLAINCONFIG[name].push({name:key,content:JOBHELPER.getJobDesc(key)});
                }

            }else if(name=="装备大全"){

                var skillobj = null;

                for(var key in _EQUIP){

                    skillobj=_IOF.createObjByName(key,null);

                    var itemdes = _IOF.getGoodsDetailStr(skillobj);
                    //itemdes+="\n出售:"+_ME.getShop(key)+"\n";
                    //itemdes+="掉落:"+_ME.getDrop(key)+"\n";
                    _ME._EXPLAINCONFIG[name].push({name:key,content:itemdes});

                }

            }else if(name=="道具大全"){
                
                for(var key in _FIGHTITEM){
                    var itemdes = "效果:"+_FIGHTITEM[key].des+"\n";
                    itemdes+="等级:"+_FIGHTITEM[key].lv+"\n";
                    //itemdes+="出售:"+_ME.getShop(key)+"\n";
                    //itemdes+="掉落:"+_ME.getDrop(key)+"\n";
                    _ME._EXPLAINCONFIG[name].push({name:key,content:itemdes});
                }
            }

            return _ME._EXPLAINCONFIG[name];
        }

    },

    getHourDes:function(){

        var hourdes = "";
        for(var i=1;i<_HONOUR._EXP.length;i++){

            hourdes+="声望:"+_HONOUR._EXP[i]+" ("+_HONOUR._TITLE1[i]+"&"+_HONOUR._TITLE2[i]+")\n"

        }

        return hourdes;
    },

    getShop:function(name){

        var arr=[];

        for(var key in _SHOP){
            if(_CU.isContain(_SHOP[key].sell,name)){
                var npclog = _GAMEDATAHELPER.getNpcLog(key);
                if(npclog&&npclog.city)
                    arr.push(key+"("+npclog.city+")");
                else
                    arr.push(key);
            }
        }

        if(arr.length>0)
        return arr.join(',');

        return "?";

    },

    getDrop:function(name){

        var arr=[];

        for(var key in _DROP){
            if(_CU.isContain(_DROP[key],name)){
                arr.push(key);
            }
        }

        for(var key in _SDROP){
            if(_CU.isContain(_SDROP[key],name)){
                arr.push(key+"(二周目)");
            }
        }

        for(var i=0;i<arr.length;i++){

            if(_ME._ARMYMAP[arr[i]]){
                arr[i]+="("+_ME._ARMYMAP[arr[i]].join(',')+")";
            }

        }

        if(arr.length>0)
            return arr.join(',');

        return "?";

    },

    //'轩辕':{job:'多宝天君',level:10,name:'轩辕',face:'轩辕',pf:'轩辕',ftype:'火',
    //dlv:110,plv:100,ilv:140,mlv:140,hlv:130,skill:['人皇','玄阳融火']},//专属
    getBossDesc:function(bossname){

        var bossConf = _NPC[bossname];

        if(bossConf){

            var content = "五行:"+bossConf.ftype;
            content += " 职业:"+bossConf.job;
            content+="\n武:"+bossConf.plv;
            content+=" 智:"+bossConf.ilv;
            content+=" 统:"+bossConf.dlv;
            content+=" 魔:"+bossConf.mlv;
            content+=" 体:"+bossConf.hlv;
            content+="\n出现地点:"+_ME.getNpcPlace(bossname);
            if(bossConf.skill&&bossConf.skill.length>0){
                content+="\n技能:";
                for(var i=0;i<bossConf.skill.length;i++){

                    var skillname = bossConf.skill[i];

                    if(_SKILL[skillname]){
                        if(_SKILL[skillname].bossflag==bossname){
                            content+="\n"+skillname+"(可掉落)";
                        }else{
                            content+="\n"+skillname;
                        }
                    }else if(_SKILLBD[skillname]){
                        if(_SKILLBD[skillname].bossflag==bossname){
                            content+="\n"+skillname+"(可掉落)";
                        }else{
                            content+="\n"+skillname;
                        }
                    }

                }
            }
            return content;
        }

        return "未知";

    },

    getBossPlace:function(bossname){

        var placeArr = [];

        for(var key in _CMAP){

            for(var i=0;i<_CMAP[key].boss.length;i++){
                if(_CMAP[key].boss[i]==bossname){
                    placeArr.push(key);
                    break;
                }
            }

        }

        for(var key in _SHOP){

            if(_SHOP[key].type=="tzs"){

                for(var i=0;i<_SHOP[key].sell.length;i++){

                    for(var j=0;j<_SHOP[key].sell[i].length;j++){

                        if(_SHOP[key].sell[i][j]==bossname+"挑战书"){
                            placeArr.push(key+""+(i+1));
                            return placeArr.join(",");
                        }

                    }

                }

            }

        }

        if(placeArr.length<=0)
        return "未知";

        return placeArr.join(",");
    }

}